# Start log file ---------------------------------------------------------------

f_time_gmt <- \() Sys.time() |> .POSIXct("GMT") |> format() |> paste("GMT")

sink("run.log", split = T)
cat(sprintf("Start replication (%s)\n", f_time_gmt()))

# Install/load packages --------------------------------------------------------

if (!require("pacman")) install.packages("pacman")

pacman::p_load(
  # utils/wrangling
  "here", "data.table", "stringr", "forcats", 
  # modeling
  "fixest", "marginaleffects", 
  # plotting
  "ggplot2", "patchwork",
  # table-making
  "modelsummary", "gt", "tinytable"
)

# Set one thread for data.table and fixest
setDTthreads(1L); setFixest_nthreads(1L)

# Create folders for outputs and intermediate data files -----------------------

for (f in c("figures", "tables", "proc")) dir.create(here(f), showWarnings = F)



# Execute scripts --------------------------------------------------------------

# Wrangling scripts
source(here("code", "1_wrangle_hr.R"))
source(here("code", "2_wrangle_cint.R"))
source(here("code", "3_wrangle_bovitz.R"))

# Analysis scripts
source(here("code", "4_analysis_tables.R"))
source(here("code", "5_analysis_figures.R"))



# End log file -----------------------------------------------------------------

cat(sprintf("\nCompleted replication (%s)\n", f_time_gmt()))
sink()
